package mahan.test.core;

import static org.junit.Assert.assertEquals;

import java.util.Arrays;
import java.util.Date;

import mahan.core.services.JSONService;
import mahan.data.model.CartLineItem;
import mahan.data.model.Customer;
import mahan.data.model.Order;
import mahan.data.model.Product;
import mahan.data.model.ShoppingCart;

import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.Before;
import org.junit.Test;

public class JSONServiceUnitTestCase {

	private JSONService service = new JSONService("localhost", 27017, "JSON");

	private void createCart() {
		Date date;
		date = new Date();
		ShoppingCart cart = new ShoppingCart(1);
		CartLineItem item1 = new CartLineItem();
		item1.setProductID(111);
		item1.setSellingPrice(23.99);
		item1.setAdded(date);
		item1.setItemType("M");
		item1.setSize(2.0);
		cart.add(item1);

		CartLineItem item2 = new CartLineItem();
		item2.setProductID(211);
		item2.setSellingPrice(53.99);
		item2.setAdded(date);
		cart.add(item2);

		service.db().persist(cart);
	}

	private void createCustomer() {
		Date date = new Date();
		Customer cust = new Customer(1, date);
		cust.setAddress("123 c");
		cust.setAdmin(true);
		cust.setCity("c");
		cust.setCountry("zogata");
		cust.setEmailAddress("a@a.com");
		cust.setFacebookID("some@some");
		cust.setFirstName("C");
		cust.setLastName("N");
		cust.setLastPurchase(date);
		cust.setLastVisit(date);
		cust.setPhoneNumber("111111");
		cust.setState("ZX");
		cust.setTwitterID("some");
		service.db().persist(cust);
	}

	private void createOrder() {
		// Add one order
		Date date = new Date();
		Order order = new Order(1, 2, date);
		order.setCountryTax(1.0);
		order.setCountyTax(0.2);
		order.setDiscount(0.3);
		order.setFulfilledOn(date);
		order.setShippingAddress("23 c");
		order.setShippingCity("z");
		order.setShippingCost(33.1);
		order.setShippingCountry("zx");
		order.setShippingEmail("a@aa");
		order.setShippingPhone("111");
		order.setShippingState("ss");
		order.setShippingZip("11111");
		order.setStateTax(0.5);
		order.setStatus("done");
		order.setTotalCost(233.1);

		service.db().persist(order);
	}

	private void createProduct() {
		Float[] bigKidSizes = new Float[] { (float) 1.0, (float) 1.5,
				(float) 2.0, (float) 2.5 };

		Product product = new Product("LAX", "Las Vegas", 111);
		product.setProductID(23);
		product.setBigKids(true);
		product.setBigKidSizes(Arrays.asList(bigKidSizes));

		product.setCostPrice(25.50);
		product.setDiscountPrice(4.25);

		product.setLittleKids(true);
		product.setLittleKidSizes(Arrays.asList(bigKidSizes));

		product.setMens(true);
		product.setMenSizes(Arrays.asList(bigKidSizes));

		product.setSellingPrice(100.34);
		product.setWomens(true);
		product.setWomenSizes(Arrays.asList(bigKidSizes));

		service.db().persist(product);
	}

	@Before
	public void populate() {
		createOrder();
		createCart();
		createCustomer();
		createProduct();
	}

	@Test
	public void testCart() throws Exception {
		JSONObject json = (JSONObject) service
				.query(new String[] { "CART", "1" });
		assertEquals(1, json.getInt("cid"));
		JSONArray arr = json.getJSONArray("i");
		assertEquals(2, arr.length());
	}

	@Test
	public void testCustomer() throws Exception {
		JSONObject json = (JSONObject) service.query(new String[] { "CUSTOMER",
				"1" });

		assertEquals(1, json.getInt("id"));
		assertEquals(Boolean.valueOf("true"), json.get("admin"));
	}

	@Test
	public void testOrder() throws Exception {
		JSONObject json = (JSONObject) service.query(new String[] { "ORDER",
				"1" });
		assertEquals(1, json.getInt("id"));
		assertEquals(2, json.getInt("cid"));
		assertEquals("done", json.getString("status"));
	}

	@Test
	public void testProduct() throws Exception {
		JSONObject json = (JSONObject) service.query(new String[] { "PRODUCT",
				"23" });
		assertEquals(23, json.getInt("pid"));
		assertEquals(25.50, json.get("cost"));
	}
}